package com.zhouwd.zplayer;

import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.os.Environment;
import androidx.annotation.Nullable;
import androidx.appcompat.app.AppCompatActivity;
import android.util.Log;
import android.view.View;

import com.alibaba.android.arouter.facade.annotation.Route;

import java.io.File;

@Route(path = "/z_media/z_player_activity")
public class ZPlayerActivity extends AppCompatActivity implements JNINotify, PreviewRender.PreviewRenderNotify {
    private static final String TAG = ZPlayerActivity.class.getSimpleName();
    private GLSurfaceView glSurfaceView;

    private PreviewRender previewRender;

    public static void instance(Activity context) {
        Intent intent = new Intent();
        intent.setClass(context, ZPlayerActivity.class);
        context.startActivity(intent);
    }

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_zplayer);

        //new Thread().isAlive()

        initView();

    }

    private void initView() {

        glSurfaceView = findViewById(R.id.activity_zplayer_sv);

        glSurfaceView.setEGLContextClientVersion(3);
        String filePath = Environment.getExternalStorageDirectory() + File.separator + "zhouwd" + File.separator + "1234567.mp4";
        Log.e(TAG, "initView: filePath > " + filePath);
        previewRender = new PreviewRender(this, filePath, this);
        glSurfaceView.setRenderer(previewRender);
        /*渲染方式，RENDERMODE_WHEN_DIRTY表示被动渲染，只有在调用requestRender或者onResume等方法时才会进行渲染。RENDERMODE_CONTINUOUSLY表示持续渲染*/
        glSurfaceView.setRenderMode(GLSurfaceView.RENDERMODE_WHEN_DIRTY);

    }

    public void clickPlay(View view) {

        JniUtil.start();

    }

    public void clickPause(View view) {


    }

    public void clickStop(View view) {



    }

    public void clickFastForward(View view) {


    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        JniUtil.release();
        JniUtil.destoryVideoFrameNotify();
    }



    @Override
    public void notifyUpdateVideoFrame() {
        Log.e(TAG, "notifyUpdateVideoFrame: 1111111");
        glSurfaceView.requestRender();
    }



    @Override
    public void init() {
        JniUtil.initVideoFrameNotify(this);
    }
}
